package com.googlecode.jaks.swt;

import static com.googlecode.jaks.common.util.StringUtil.isEmpty;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.lang.SystemUtils;
import org.codehaus.plexus.util.FileUtils;

/**
 * 
 * @author Jason Smith
 * @requiresDependencyResolution compile
 * @goal install-swt-artifact-from-http
 * @phase package
 */
public class InstallSwtArtifactFromHttpMojo extends InstallArtifactFromHttpMojo 
{
	protected void processArchive(final File archive) throws FileNotFoundException, IOException
	{
		final File folder = unzipArchive(archive);
		
		if(!isEmpty(classifier))
		{
			attachArtifact(project, "jar", classifier, new File(folder, "swt.jar"));
			attachArtifact(project, "jar", classifier + "-debug", new File(folder, "swt-debug.jar"));
		}
		
		if(classifier.equals(getPrimary()))
		{
			FileUtils.copyFile(
					new File(folder, "swt.jar"), 
					new File(new File(project.getBuild().getDirectory()), project.getArtifactId() + "-" + project.getVersion() + ".jar"));
			attachArtifact(project, "jar", "debug", new File(folder, "swt-debug.jar"));
		}
	}
	
	protected String getPrimary()
	{
		if(SystemUtils.IS_OS_LINUX)
		{
			if(Arrays.asList("amd64", "x86_64").contains(SystemUtils.OS_ARCH))
			{
				return "gtk-linux-x86_64";
			}
			else if("x86".equals(SystemUtils.OS_ARCH))
			{
				return "gtk-linux-x86";
			}
		}
		else if(SystemUtils.IS_OS_WINDOWS)
		{
			if(Arrays.asList("amd64", "x86_64").contains(SystemUtils.OS_ARCH))
			{
				return "windows-x86_64";
			}
			else if("x86".equals(SystemUtils.OS_ARCH))
			{
				return "windows-x86";
			}
		}
		else if(SystemUtils.IS_OS_MAC_OSX)
		{
			if(Arrays.asList("amd64", "x86_64").contains(SystemUtils.OS_ARCH))
			{
				return "cocoa-macosx-x86_64";
			}
			else if("x86".equals(SystemUtils.OS_ARCH))
			{
				return "cocoa-macosx";
			}
		}
		
		return "";
	}
}
